<template>
  <div class="background-container">
    <div class="user-login">
      <el-container>
        <el-header>管理员登录</el-header>
        <el-main>
          <el-input placeholder="账号" v-model="users.username" clearable></el-input>
          <el-input placeholder="密码" v-model="users.password" clearable show-password></el-input>
        </el-main>
        <el-footer>
          <el-button type="primary" @click="login()">登录</el-button>
        </el-footer>
      </el-container>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";
export default {
  name: "UserLogin",
  data() {
    return {
      users: {
        username: "",
        password: "",
        token: ""
      }
    };
  },
  methods: {
    login() {
      this.users.password = md5(this.users.password+"lx_qy");
      console.log(this.users.password);
      // this.users.password = md5(this.users.password);
      if (this.users.username != "" && this.users.password != "") {
        this.$axios.post("http://47.108.248.93:999/login", this.users).then(res => {
          console.log(res.data);
          if (res.data.code == 212) {
            window.localStorage.setItem("token", res.data.data);
            window.localStorage.setItem("username", this.users.username);
            this.$message({
              message: "登录成功" + "欢迎 " + this.users.username,
              type: "success"
            });
            if (this.$router.currentRoute.path !== "/home") {
              setTimeout(() => {
                this.$router.push({
                  path: "/home",
                  query: {
                    username: this.users.username
                  }
                });
              }, 1000);
            }
          } else {
            this.$message({
              message: "登录失败",
              type: "warning"
            });
          }
        });
      } else {
        this.$message({
          message: "账号或密码不能为空",
          type: "warning"
        });
      }
    }
  },
  created() {
    const token = window.localStorage.getItem("token");
    if (token != null) {
      console.log(token);
      this.$axios.post("http://47.108.248.93:999/tokenLogin", token).then(res => {
        console.log(res.data);
        if (res.data.code == 212) {
          window.localStorage.setItem("username", res.data.data);
          this.$message({
            message: "token登录成功 正在跳转" + "欢迎 " + res.data.data,
            type: "success"
          });
          if (this.$router.currentRoute.path !== "/home") {
            setTimeout(() => {
              this.$router.push({
                path: "/home",
                query: {
                  username: res.data.data
                }
              });
            }, 1000);
          }
        } else {
          this.$message({
            message: "token失效 重新登陆",
            type: "warning"
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.background-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("../assets/login.jpg") no-repeat center center fixed;
  background-size: cover;
}

.user-login {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30%;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.5); /* 增加透明效果 */
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.el-header,
.el-footer {
  background-color: transparent;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-header {
  font-size: 24px; /* 增大字体 */
  font-weight: bold; /* 字体加粗 */
  margin-bottom: 20px; /* 增加底部外边距 */
}

.el-main {
  background-color: transparent;
  color: #333;
  text-align: center;
  line-height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.el-main .el-input {
  margin-bottom: 0px; /* 增加输入框之间的距离 */
  width: 80%;
}

.el-container {
  margin-bottom: 20px;
}

.el-button {
  font-size: 16px; /* 增大按钮字体 */
  padding: 10px 20px; /* 增加按钮内边距 */
}
</style>

